﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DTOService;
using System.Data;

namespace DAOService
{
    public class DAOLoaiSanPham : IDAOLoaiSanPham
    {
        Provider p = new Provider();

        public LoaiSanPham[] SelectLoaiSanPham()
        {
            LoaiSanPham[] rs;
            try
            {
                DataTable dt = p.ExecuteStoreProcedure("sp_DanhSachLoaiSanPham");
                rs = new LoaiSanPham[dt.Rows.Count];
                LoaiSanPham temp;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    temp = new LoaiSanPham();
                    temp.MaLoaiSanPham = int.Parse(dt.Rows[i][0].ToString());
                    temp.TenLoaiSanPham = dt.Rows[i][1].ToString();
                    temp.PhichoThue = float.Parse(dt.Rows[i][2].ToString());
                    temp.DanhDauXoa = int.Parse(dt.Rows[i][3].ToString());
                    rs[i] = temp;
                }
            }
            catch (Exception ex)
            { throw new Exception("Error: " + ex.Message); }
            return rs;
        }

        public int UpdateLoaiSanPham(LoaiSanPham parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_CapNhatLoaiSanPham", new string[] { "maloaisanpham","tenloaisanpham", "phichothue" }, new object[] { parr.MaLoaiSanPham, parr.TenLoaiSanPham, parr.PhichoThue });
            return int.Parse(dt.Rows[0][0].ToString());
        }

        public int DeleteLoaiSanPham(LoaiSanPham parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_XoaLoaiSanPham", new string[] { "maloaisanpham" }, new object[] { parr.MaLoaiSanPham });
            return int.Parse(dt.Rows[0][0].ToString());
        }
        public int InsertLoaiSanPham(LoaiSanPham parr)
        {
            DataTable dt = p.ExecuteStoreProcedure("sp_ThemLoaiSanPham", new string[] { "tenloaisanpham", "phichothue" }, new object[] { parr.TenLoaiSanPham, parr.PhichoThue });
            return int.Parse(dt.Rows[0][0].ToString());
        }

        public LoaiSanPham GetLoaiSanPham(int id)
        {
            LoaiSanPham rs = new LoaiSanPham();
            LoaiSanPham[] ls = this.SelectLoaiSanPham();
            foreach (LoaiSanPham v in ls)
            {
                if (v.MaLoaiSanPham == id)
                {
                    rs = v;
                    break;
                }
            }
            return rs;
        }
    }
}
